package com.leetcode;

/**
 * @author fy
 * @date 2022/3/29 19:35
 */
public class Solution198 {

    /**
     * 198. 打家劫舍
     *
     * @param nums
     * @return
     */
    public int rob(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int n = nums.length;
        int first = nums[0];
        if (nums.length == 1) {
            return first;
        }
        int second = Math.max(nums[0], nums[1]);
        for (int i = 2; i < n; i++) {
            int tmp = second;
            second = Math.max(first + nums[i], second);
            first = tmp;
        }
        return second;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{2, 7, 9, 3, 1};
        new Solution198().rob(nums);
    }

}
